﻿using HR60_PanelSoft.Model;
using HR60_PanelSoft.NetWork.Base;
using HR60_PanelSoft.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HR60_PanelSoft.NetWork.Sender
{
    class Sender_0101:BaseSender
    {
        public Sender_0101(byte panelNo, byte sceneNum, Scene scene, Device device)
        {

            byte[] data = new byte[128]; //32个分机


            List<Device> listDevice = device.childDevices;
            int cur = 0;
            foreach (Device cd in listDevice)
            {


                SceneEvent se = scene.findEventByDevice(cd.uuid);
                //1.无
                if (se == null)
                {
                    cur = cur + 4;
                    continue;
                }

                List<string> sArrts = se.attrs;

                List<string> values = se.attrValueKeys;



                DeviceType type = cd.type;
                List<CtrlAttr> attrs = type.attrs;
                if (attrs == null)
                {
                    cur = cur + 4;
                    continue;
                }

                if (attrs.Count != 5)
                {
                    throw new Exception("数据内容有问题,请查看新风属性是否配置正确？");
                }


                if (attrs.Count == 5)
                {

                    string binaryString = "1";//待拼接的字符串
                

                    //1.
                    {
                        CtrlAttr attr0 = attrs[0];
                        int index = sArrts.IndexOf(attr0.name);
                        string value0 = null;
                        if (index != -1)
                        {
                            value0 = values[index];
                        }


                        if (value0 == null)  //开关机
                        {
                            binaryString = binaryString + "0";
                        }
                        else
                        {
                            binaryString = binaryString + value0;
                        }

                    }

                    //2
                    {
                        CtrlAttr attr1 = attrs[1];// 工作模式
                        int index = sArrts.IndexOf(attr1.name);
                        string value1 = null;
                        if (index != -1)
                        {
                            value1 = values[index];
                        }

                        if (value1 == null)
                        {
                            binaryString = binaryString + "000";
                        }
                        else if (value1 == "1")
                        {
                            binaryString = binaryString + "001";
                        }
                        else if (value1 == "2")
                        {
                            binaryString = binaryString + "010";
                        }

                    }




                    //3
                    {

                        CtrlAttr attr2 = attrs[2];//风速
                        int index = sArrts.IndexOf(attr2.name);
                        string value2 = null;
                        if (index != -1)
                        {
                            value2 = values[index];
                        }

                        if (value2 == null)
                        {
                            binaryString = binaryString + "000";
                        }
                        else if (value2 == "1")
                        {
                            binaryString = binaryString + "000";
                        }
                        else if (value2 == "2")
                        {
                            binaryString = binaryString + "001";
                        }
                        else if (value2 == "3")
                        {
                            binaryString = binaryString + "010";
                        }

                    }

                  
                   

                    data[cur++] = Convert.ToByte(binaryString, 2);//放值


                    binaryString = "";//清空内容

                    //4.
                    {  
                        //热交换开关
                        CtrlAttr attr3 = attrs[3];
                        int index = sArrts.IndexOf(attr3.name);
                        string value3 = null;
                        if (index != -1)
                        {
                            value3 = values[index];
                        }
                        if (value3 == null)
                        {
                            binaryString = binaryString + "0";
                        }
                        else
                        {
                            binaryString = binaryString + value3;
                        }

                    }
                   


                    binaryString = binaryString + "0000";

                    //5
                    {
                        CtrlAttr attr4 = attrs[4];
                        int index = sArrts.IndexOf(attr4.name);
                        string value4 = null;
                        if (index != -1)
                        {
                            value4 = values[index];
                        }
                        if (value4 == null)
                        {
                            binaryString = binaryString + "0";
                        }
                        else if (value4 == "0")
                        {
                            binaryString = binaryString + "000";
                        }
                        else if (value4 == "1")
                        {
                            binaryString = binaryString + "001";
                        }
                        else if (value4 == "2")
                        {
                            binaryString = binaryString + "010";
                        }
                        else if (value4 == "3")
                        {
                            binaryString = binaryString + "011";
                        }
                        else if (value4 == "4")
                        {
                            binaryString = binaryString + "100";
                        }
                        else if (value4 == "5")
                        {
                            binaryString = binaryString + "101";
                        }

                    }
                   

                    data[cur++] = Convert.ToByte(binaryString, 2);//放值


                    data[cur++] = 0;
                    data[cur++] = 0;

                }


            }

            //构造package包
            this.package = Package.getPackage(0x01, 0x01, panelNo, sceneNum, data);




        }
    }
}
